﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace GMUtilitiesDotNet.AttachedProperties
{
    /// <summary>
    /// Static class holding all IStringValidation implementations
    /// </summary>
    public static class StringValidation
    {
        public static IStringValidation DecimalValidation { get; private set; }
        public static IStringValidation PositiveIntegerValidation { get; private set; }

        static StringValidation()
        {
            DecimalValidation = new DecimalValidation();
            PositiveIntegerValidation = new PositiveIntegerValidation();
        }
    }

    /// <summary>
    /// Interface representing string validations.
    /// String validations tell if any string is valid or not depending on some specific rules
    /// </summary>
    public interface IStringValidation
    {
        /// <summary>
        /// Checks if input string is valid
        /// </summary>
        /// <param name="inputString">Input string</param>
        /// <returns>Returns true if string is valid, false otherwise</returns>
        bool IsValid(string inputString);
    }

    /// <summary>
    /// String validation which supports positive and negative decimal numbers
    /// </summary>
    public class DecimalValidation : IStringValidation
    {
        public bool IsValid(string inputString)
        {
            bool match = false;

            if (!String.IsNullOrEmpty(inputString))
            {
                Regex regExpr = new Regex(@"^([-+]?)(\d*)([,.]?)(\d*)$");
                match = regExpr.IsMatch(inputString);
            }
            
            return match;
        }
    }

    /// <summary>
    /// String validation which supports positive integer numbers
    /// </summary>
    public class PositiveIntegerValidation : IStringValidation
    {
        public bool IsValid(string inputString)
        {
            bool match = false;

            if (!String.IsNullOrEmpty(inputString))
            {
                Regex regExpr = new Regex(@"^(\d*)$");
                match = regExpr.IsMatch(inputString);
            }

            return match;
        }
    }
}
